﻿package communication1.pfl;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Vibrator;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

public class SendActivity extends Activity {
	private Thread thread;
	
	private Handler handler;
	private EditText desc = null;
	private EditText remark_content = null;
	private TextView capture = null,record_video=null;
	private LinearLayout hidden_area = null;
	private MySQLiteOpenHelper myOpenHelper;// 创建一个继承SQLiteOpenHelper类实例
	private SQLiteDatabase mysql;
	private TableRow tb_capture = null,tb_record = null;

	protected int biaozhi=0;
	String rain="";;
	String wide="";
	String undergrand="";
	String safe="";
	String beizhu="";
	String jiancedianhaoma="123456789012345";

	private int jinduzhi1;

	private int flagp=0;

	private int flagv=0;

	private long jinduzhi1_max;

	private String Number2="";

	protected String pic_name="";

	protected String zhaopianlujing="";

	protected String video_name="";

	protected String shipinlujing="";

	private ProgressBar jindu1;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.remark);
		TelephonyManager phoneMgr = (TelephonyManager) this
				.getSystemService(Context.TELEPHONY_SERVICE);
		Number2 = phoneMgr.getDeviceId();
		myOpenHelper = new MySQLiteOpenHelper(this);// 实例一个数据库辅助器
		mysql = myOpenHelper.getWritableDatabase(); // 实例数据库
		jindu1 = (ProgressBar) findViewById(R.id.jindu1);
		thread = new Thread(new Runnable()// 线程已经测试通过
		{
			/* @Override */
			public void run() {
				while (true) {
					//Log.v("pfl","线程关闭");
					if (biaozhi == 1)// 开启监测点信息上传线程
					{
                        Log.v("pfl","进入线程");
						biaozhi = 0;

						if (rain.length() == 0) {
							rain = "未录入";
						}
						if (wide.length() == 0) {
							wide = "0";
						}
						if (undergrand.length() == 0) {
							undergrand = "未录入";
						}
						if (safe.length() == 0) {
							safe = "未录入";
						}
						if (beizhu.length() == 0) {
							beizhu = "未录入";
						}
						Cursor guangbiao = mysql.rawQuery(
								"select distinct * from shezhi", null);// 执行Select语句

						guangbiao.moveToNext();

						String a1 = guangbiao.getString(0);

						String a2 = guangbiao.getString(1);
						 Log.v("pfl","查询数据库成功");
						a1="172.19.10.50";
						a2="8080";
						senddata("http://" + a1 + ":" + a2
								+ "/webreceiver/servlet/fileload",
								pic_name, zhaopianlujing, video_name,
								shipinlujing);

					}
					
					}
				}
			
		});



        thread.start();
		initView();
		
	}
	public void senddata(String urlStr, String serverFileName1, String path1,
			String serverFileName2, String path2) {
		jinduzhi1 = 0;
		if (flagp == 1 && flagv == 0) {
			File uploadFile1 = new File(path1);
			long size = (uploadFile1.length()) / 1024;
			Log.v("pfl", "max长度是：" + (Long.toString(size)));
			jinduzhi1_max = size + 1;
		}
		if (flagp == 1 && flagv == 1) {
			File uploadFile1 = new File(path1);
			long size1 = (uploadFile1.length()) / 1024;

			File uploadFile2 = new File(path2);
			long size2 = (uploadFile2.length()) / 1024;

			long size = (size1 + size2);

			Log.v("pfl", "max长度是：" + (Long.toString(size)));
			jinduzhi1_max = size + 2;
		}
		if (flagp == 0 && flagv == 1) {
			File uploadFile2 = new File(path2); // 不多讲
			long size = (uploadFile2.length()) / 1024;
			Log.v("pfl", "max长度是：" + (Long.toString(size)));
			jinduzhi1_max = size + 1;
		}
		if (flagp == 0 && flagv == 0) {
			Log.v("pfl", "没有录入多媒体信息");
		}

		long shen = jinduzhi1_max * 6 / 5;
		long chazhi = jinduzhi1_max * 1 / 5;
		jinduzhi1_max = shen;
		jindu1.setMax((int) jinduzhi1_max);

		String end = "\r\n";
		String twoHyphens = "--";
		String boundary = "*****";
		try {
			URL url = new URL(urlStr);
			HttpURLConnection con = (HttpURLConnection) url.openConnection();
			Log.v("pfl", "333333");

			con.setDoInput(true);
			con.setDoOutput(true);
			con.setUseCaches(false);

			con.setRequestMethod("POST");

			con.setRequestProperty("Connection", "Keep-Alive");
			con.setRequestProperty("Charset", "UTF-8");
			con.setRequestProperty("Content-Type",
					"multipart/form-data;boundary=" + boundary);
			Log.v("pfl", "444");
			con.connect();

			Log.v("pfl", "555");
			DataOutputStream ds = new DataOutputStream(con.getOutputStream());
			Log.v("pfl", "666");

			ds.writeBytes(twoHyphens + boundary + end);
			ds.writeBytes("Content-Disposition: form-data; " + "name=\"flag\""
					+ end + end);
			ds.writeBytes("11" + end);

			ds.writeBytes(twoHyphens + boundary + end);
			ds.writeBytes("Content-Disposition: form-data; "
					+ "name=\"Number2\"" + end + end);
			ds.writeBytes(Number2 + end);

			ds.writeBytes(twoHyphens + boundary + end);
			ds.writeBytes("Content-Disposition: form-data; "
					+ "name=\"jiancedianhaoma\"" + end + end);
			ds.writeBytes(jiancedianhaoma + end);

			ds.writeBytes(twoHyphens + boundary + end);
			ds.writeBytes("Content-Disposition: form-data; " + "name=\"rain\""
					+ end + end);
			ds.write(rain.getBytes());
			ds.writeBytes(end);

			ds.writeBytes(twoHyphens + boundary + end);
			ds.writeBytes("Content-Disposition: form-data; " + "name=\"wide\""
					+ end + end);
			ds.write(wide.getBytes());
			ds.writeBytes(end);

			ds.writeBytes(twoHyphens + boundary + end);
			ds.writeBytes("Content-Disposition: form-data; "
					+ "name=\"undergrand\"" + end + end);
			ds.write(undergrand.getBytes());
			ds.writeBytes(end);

			ds.writeBytes(twoHyphens + boundary + end);
			ds.writeBytes("Content-Disposition: form-data; " + "name=\"safe\""
					+ end + end);
			ds.write(safe.getBytes());
			ds.writeBytes(end);

			ds.writeBytes(twoHyphens + boundary + end);
			ds.writeBytes("Content-Disposition: form-data; "
					+ "name=\"beizhu\"" + end + end);
			ds.write(beizhu.getBytes());
			ds.writeBytes(end);

			if (flagp == 1) {
				flagp = 0;
				ds.writeBytes(twoHyphens + boundary + end);
				ds.writeBytes("Content-Disposition: form-data; "
						+ "name=\"picture\";filename=\"" + serverFileName1
						+ "\"" + end);
				ds.writeBytes(end);

				File uploadFile1 = new File(path1); // 不多讲
				FileInputStream fStream = new FileInputStream(uploadFile1);

				int bufferSize = 1024;
				byte[] buffer = new byte[bufferSize];
				int length = -1;

				while ((length = fStream.read(buffer)) != -1) {
					// Log.v("pfl","读取图片文件中,:"+length+"b");
					jindu1.setProgress((int) jinduzhi1);
					jinduzhi1++;
					Log.v("pfl", "jinduzhi1:" + (int) jinduzhi1);
					ds.write(buffer, 0, length);
				}
				fStream.close();
				ds.writeBytes(end);

			}
			if (flagv == 1) {
				flagv = 0;

				ds.writeBytes(twoHyphens + boundary + end);
				ds.writeBytes("Content-Disposition: form-data; "
						+ "name=\"vidio\";filename=\"" + serverFileName2 + "\""
						+ end);
				ds.writeBytes(end);
				/* 取得文件的FileInputStream */
				File uploadFile2 = new File(path2); // 不多讲
				FileInputStream fStream = new FileInputStream(uploadFile2);
				/* 设置每次写入1024bytes */
				int bufferSize = 1024;
				byte[] buffer = new byte[bufferSize];
				int length = -1;
				/* 从文件读取数据至缓冲区 */
				while ((length = fStream.read(buffer)) != -1) {
					// Log.v("pfl","读取视频文件中,:"+length+"b");
					jindu1.setProgress((int) jinduzhi1);
					jinduzhi1++;
					Log.v("pfl", "jinduzhi1:" + (int) jinduzhi1);
					/* 将资料写入DataOutputStream中 */
					ds.write(buffer, 0, length);
				}
				/* close streams */
				fStream.close();
			}
			ds.writeBytes(end);
			ds.writeBytes(twoHyphens + boundary + twoHyphens + end);
			ds.flush();

			/* 取得Response内容 */
			InputStream is = con.getInputStream();
			Log.v("pfl", "获取输入流成功");
			int ch;
			StringBuffer b = new StringBuffer();
			Log.v("pfl", "创建缓存成功");
			while ((ch = is.read()) != -1) {
				b.append((char) ch);
				Log.v("pfl", "读取输入流一次");
			}
			Log.v("pfl", "读取完成：" + b);
			if (b.toString() != "") {
				jindu1.setProgress((int) (jinduzhi1 + chazhi));
				Message msg1033 = new Message();
				msg1033.what = 1033; // 插入数据库是否成功
				handler.sendMessage(msg1033);
				Vibrator vib = (Vibrator) getSystemService(VIBRATOR_SERVICE);

				vib.vibrate(100);
				jinduzhi1 = 0;
				jinduzhi1_max = 0;
			}
			/* 关闭DataOutputStream */

			ds.close();

		} catch (Exception e) {
			Message msg1034 = new Message();
			msg1034.what = 1034; // 插入数据库是否成功
			handler.sendMessage(msg1034);
			// Toast.makeText(getApplicationContext(),
			// e.getMessage().toString().trim(),Toast.LENGTH_SHORT).show();
		}
	}


	private void initView() {
		// TODO Auto-generated method stub
		findViewById(R.id.finish).setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				setResult(RESULT_OK);
				finish();
			}
		});
		
		{
			desc =(EditText) findViewById(R.id.desc);
			remark_content = (EditText) findViewById(R.id.remark_content);
			capture =(TextView) findViewById(R.id.capture);
			record_video =(TextView) findViewById(R.id.record_video);		
			hidden_area = (LinearLayout) findViewById(R.id.hidden_area);
			hidden_area.setVisibility(View.GONE);
			tb_record = (TableRow) findViewById(R.id.tb_record);
			tb_record.setVisibility(View.GONE);
			tb_capture = (TableRow) findViewById(R.id.tb_capture);
			tb_capture.setVisibility(View.GONE);
		}
		{
			
			findViewById(R.id.begin_record).setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					 rain=getIntent().getExtras().getCharSequence( "rain" ).toString() ;
					 wide=getIntent().getExtras().getCharSequence( "fengkuan" ).toString() ;
					 undergrand=getIntent().getExtras().getCharSequence( "undergraund" ).toString() ;
					 safe=getIntent().getExtras().getCharSequence( "safe" ).toString() ;
					 beizhu=remark_content.getText().toString();
					 zhaopianlujing=getIntent().getExtras().getCharSequence( "zhaopianlujing" ).toString() ;
					 pic_name=getIntent().getExtras().getCharSequence( "pic_name" ).toString() ;
					 shipinlujing=getIntent().getExtras().getCharSequence( "shipinlujing" ).toString() ;
					 video_name=getIntent().getExtras().getCharSequence( "video_name" ).toString() ;
					 flagp=Integer.parseInt(getIntent().getExtras().getCharSequence( "flagp" ).toString()) ;
					 flagv=Integer.parseInt(getIntent().getExtras().getCharSequence( "flagv" ).toString()) ;
					Toast.makeText(SendActivity.this, "雨量:"+rain, Toast.LENGTH_SHORT).show();
					Toast.makeText(SendActivity.this, "封宽:"+wide, Toast.LENGTH_SHORT).show();
					Toast.makeText(SendActivity.this, "地下水:"+undergrand, Toast.LENGTH_SHORT).show();
					Toast.makeText(SendActivity.this, "危险度:"+safe, Toast.LENGTH_SHORT).show();
					Toast.makeText(SendActivity.this, "备注:"+remark_content.getText().toString(), Toast.LENGTH_SHORT).show();
					Toast.makeText(SendActivity.this, "zhaopianlujing:"+zhaopianlujing, Toast.LENGTH_SHORT).show();
					Toast.makeText(SendActivity.this, "pic_name:"+pic_name, Toast.LENGTH_SHORT).show();
					Toast.makeText(SendActivity.this, "shipinlujing:"+shipinlujing, Toast.LENGTH_SHORT).show();
					Toast.makeText(SendActivity.this, "video_name:"+video_name, Toast.LENGTH_SHORT).show();
					Toast.makeText(SendActivity.this, "flagp:"+flagp, Toast.LENGTH_SHORT).show();
					Toast.makeText(SendActivity.this, "flagv:"+flagv, Toast.LENGTH_SHORT).show();
					Log.v("pfl","准备开启线程");
					biaozhi=1; 
				}
			});
			
			findViewById(R.id.goback).setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					finish();
				}
			});
			
		}
	}
}
